﻿using RayGraphComponent;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace DemoApp
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        Serie serie1;
        Serie serie2;
        int x = 0;

        Random r = new Random();

        public MainPage()
        {
            this.InitializeComponent();

            PutData();
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void PutData()
        {
            List<Point> data1 = new List<Point>();
            data1.Add(new Point(0, 0));
            data1.Add(new Point(1, 100));
            data1.Add(new Point(2, 90));
            data1.Add(new Point(3, 160));
            data1.Add(new Point(4, 150));
            data1.Add(new Point(5, 180));
            data1.Add(new Point(6, 90));
            data1.Add(new Point(7, 125));
            data1.Add(new Point(8, 200));
            data1.Add(new Point(9, 150));
            data1.Add(new Point(10, 180));
            data1.Add(new Point(11, 90));
            data1.Add(new Point(12, 125));
            data1.Add(new Point(13, 200));
            data1.Add(new Point(14, 150));
            data1.Add(new Point(15, 120));
            data1.Add(new Point(16, 125));
            data1.Add(new Point(17, 100));
            data1.Add(new Point(18, 180));
            data1.Add(new Point(19, 90));
            data1.Add(new Point(20, 125));
            data1.Add(new Point(21, 200));

            serie1 = new Serie(this.Graph1, "Static Chart");
            serie1.ShiftSize = 100;
            serie1.SetData(data1);

            Graph1.AutoRedraw = true;
            Graph1.AddSerie(serie1);

            List<Point> data2 = new List<Point>();
            data2.Add(new Point(0, 0));

            serie2 = new Serie(this.Graph2, "Dynamic Chart");
            serie2.ShiftSize = 50;
            serie2.SetData(data2);

            Graph2.AddSerie(serie2);

            // FOR DYNAMIC DATA:
            DispatcherTimer t = new DispatcherTimer();
            t.Interval = TimeSpan.FromSeconds(0.8);
            t.Tick += t_Tick;
            t.Start();
        }

        void t_Tick(object sender, object e)
        {
            //DispatcherTimer t = sender as DispatcherTimer;

            int k = (x % 2 == 0) || (x % 3 == 0) || (x % 4 == 0) || (x % 5 == 0) || (x % 6 == 0) ? 5 : 20;
            int s = k == 5 ? 100 : 80;

            Point p = new Point();
            p.X = x++;
            p.Y = Math.Round(k + r.NextDouble() * k);

            serie2.AddPoint(p, true);
            Graph2.Redraw();
        }
    }
}
